package com.icss.ah.distributionPerson.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icss.j2ee.util.UUID;
@Service
@Transactional
public class DistributionPersonService {
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;

	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	public List queryDistributionPersonList(HashMap map) {
		List list = new ArrayList();
		try{
			//根据登录账号获取账号基本信息
			list = sqlSessionTemplate.selectList("DistributionPersonBean.queryDistributionPersonList", map);
			infolog.info("DistributionPersonService|queryDistributionPersonList|list:"+list);
			list = list == null ? new ArrayList() : list;
		} catch (Exception e) {
			e.printStackTrace();
			list = new ArrayList();
			errlog.error("DistributionPersonService|queryDistributionPersonList", e);
		}
		return list;
		
	}
	
	public Long queryDistributionPersonCount(HashMap map) {
		Long reValue = 0l;
		try {
			reValue = (Long)sqlSessionTemplate.selectOne("DistributionPersonBean.queryDistributionPersonCount", map);
			infolog.info("DistributionPersonService|queryDistributionPersonCount|reValue:"+reValue);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("DistributionPersonService|queryDistributionPersonCount", e);
		} 
		return reValue;
	}
	
	public HashMap queryPersonMap(HashMap map) {
		HashMap personMap=new HashMap();
		try{
			personMap = sqlSessionTemplate.selectOne("DistributionPersonBean.queryPersonMap", map);
			infolog.info("DistributionPersonService|queryPersonMap|vehicleMap:"+personMap);
		} catch (Exception ex) {
			ex.printStackTrace();
			errlog.error("DistributionPersonService|queryPersonMap", ex);
		} 
		return personMap == null ? new HashMap() : personMap;
	}
	
	
	public boolean savePerson(HashMap map,String userUuid,String operat){
		boolean reValue = false;
		try {
			if(userUuid.equals("")||userUuid==null){
				map.put("userUuid",new UUID().toString() );
			}else{
				map.put("userUuid",userUuid);
			}
			if(operat.equals("add")){
				sqlSessionTemplate.insert("DistributionPersonBean.insertPerson", map);
			}else if(operat.equals("edit")){
				sqlSessionTemplate.update("DistributionPersonBean.updatePerson", map);
			}
			reValue = true;
			infolog.info("DistributionPersonService|savePerson|reValue:"+reValue);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("DistributionPersonService|savePerson", e);
		} 
		return reValue;
	}
	
	public boolean deletePerson(HashMap map) {
		boolean reValue = false;
		try {
			int f = sqlSessionTemplate.delete("DistributionPersonBean.deletePerson", map);
			infolog.info("DistributionPersonService|deletePerson|f:"+f);
			reValue = true;
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("DistributionPersonService|deletePerson", e);
		} 
		return reValue;
	}
	
	public boolean dealStatus(HashMap map) {
		boolean reValue = false;
		try {
			int f = sqlSessionTemplate.update("DistributionPersonBean.dealStatus", map);
			infolog.info("DistributionPersonService|dealStatus|f:"+f);
			reValue = true;
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("DistributionPersonService|dealStatus", e);
		} 
		return reValue;
	}
	/**
	 * 重置密码
	 * @param map
	 * @return
	 */
	public boolean resetPassword(HashMap map) {
		boolean reValue = false;
		try {
			int f = sqlSessionTemplate.update("DistributionPersonBean.resetPassword", map);
			if(f>0){
				reValue = true;
			}
			infolog.info("DistributionPersonService|resetPassword|f:"+f);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("DistributionPersonService|resetPassword", e);
		} 
		return reValue;
	}
	/**
	 * 验证账号是否存在
	 * @param map
	 * @return
	 */
	public boolean checkUserAccountExist(HashMap map) {
		boolean reValue = false;
		try {
			Integer count = sqlSessionTemplate.selectOne("DistributionPersonBean.checkUserAccountExist", map);
			if(count==0){
				reValue = true;
			}
			infolog.info("DistributionPersonService|checkUserAccountExist|count:"+count);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("DistributionPersonService|checkUserAccountExist", e);
		} 
		return reValue;
	}
}
